package com.briup.es.config.response;

import lombok.AccessLevel;
import lombok.Data;
import lombok.RequiredArgsConstructor;

/**
 * 统一响应实体（格式）
 */
@Data
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class Result<T> {
    /**
     * 响应状态码
     */
    private final Integer code;
    /**
     * 响应消息
     */
    private final String message;
    /**
     * 响应数据
     */
    private final T data;

    private Result(){
        this(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    private Result(Integer code,String message){
        this(code,message,null);
    }

    /**
     * 没有响应数据的成功
     * @return Result<T>
     */
    public static <T> Result<T> success(){
        return new Result<>();
    }

    /**
     * 有响应数据的成功
     * @param data 响应数据
     * @param <T> 响应数据的类型
     * @return 响应
     */
    public static <T> Result<T> success(T data){
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }

    public static Result<Void> fail(String message){
        return new Result<>(500,message);
    }

    public static Result<Void> fail(Integer code,String message){
        return new Result<>(code,message);
    }
}
